package org.penguin.study.bk.base.util;

import java.math.BigDecimal;

/**
 * @author chengyinghua
 */
public class ObjectUtil {

    public static int getInt(String s, int defaultValue) {
        if (s == null || s == "") {
            return (defaultValue);
        }
        try {
            return (Integer.parseInt(s));
        } catch (NumberFormatException e) {
            return (defaultValue);
        }
    }

    public static int getInt(String s) {
        if (s == null || s == "") {
            return 0;
        }
        try {
            return (Integer.parseInt(s));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static int getInt(String s, Integer defaultValue) {
        if (s == null || s == "") {
            return defaultValue;
        }
        try {
            return (Integer.parseInt(s));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static Integer[] getInts(String[] s) {
        Integer[] integer = new Integer[s.length];
        if (s == null) {
            return null;
        }
        for (int i = 0; i < s.length; i++) {
            integer[i] = Integer.parseInt(s[i]);
        }
        return integer;

    }

    public static double getDouble(String s, double defaultValue) {
        if (s == null || s == "") {
            return (defaultValue);
        }
        try {
            return (Double.parseDouble(s));
        } catch (NumberFormatException e) {
            return (defaultValue);
        }
    }

    public static double getDouble(Double s, double defaultValue) {
        if (s == null) {
            return (defaultValue);
        }
        return s;
    }

    public static int getInt(Object object, int defaultValue) {
        if (StringUtil.isEmpty(object)) {
            return (defaultValue);
        }
        try {
            return (Integer.parseInt(object.toString()));
        } catch (NumberFormatException e) {
            return (defaultValue);
        }
    }

    public static Integer getInt(Object object) {
        if (StringUtil.isEmpty(object)) {
            return null;
        }
        try {
            return (Integer.parseInt(object.toString()));
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static int getInt(BigDecimal s, int defaultValue) {
        if (s == null) {
            return (defaultValue);
        }
        return s.intValue();
    }

    public static Integer[] getIntegerArray(String[] object) {
        int len = object.length;
        Integer[] result = new Integer[len];
        try {
            for (int i = 0; i < len; i++) {
                result[i] = new Integer(object[i].trim());
            }
            return result;
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static String getString(String s) {
        return (getString(s, ""));
    }

    public static String getString(Object object) {
        if (StringUtil.isEmpty(object)) {
            return "";
        }
        return (object.toString().trim());
    }

    public static String getString(int i) {
        return (String.valueOf(i));
    }

    public static String getString(float i) {
        return (String.valueOf(i));
    }

    public static String getString(String s, String defaultValue) {
        if (StringUtil.isEmpty(s)) {
            return (defaultValue);
        }
        return (s.trim());
    }

    public static String getString(Object s, String defaultValue) {
        if (StringUtil.isEmpty(s)) {
            return (defaultValue);
        }
        return (s.toString().trim());
    }

    public static long stringToLong(String str) {
        Long test = new Long(0);
        try {
            test = Long.valueOf(str);
        } catch (Exception e) {
        }
        return test.longValue();
    }
}
